package com.lam.system.service.impl;

import java.util.List;
import java.util.Objects;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.lam.common.utils.StringUtils;
import com.lam.common.validate.ValidateUtil;

import org.springframework.stereotype.Service;
import com.lam.system.mapper.SysDeptMapper;
import com.lam.system.entity.SysDept;
import com.lam.system.param.SysDeptParam;
import com.lam.system.service.SysDeptService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

/**
 * 组织机构Service业务层处理
 * 
 * @author admin
 * @date 2022-10-18
 */
@Service
public class SysDeptServiceImpl  implements SysDeptService {


    @Autowired
    private SysDeptMapper sysDeptMapper;

    /**
     * 查询组织机构
     * 
     * @param id ID
     * @return 单个对象
     */
    @Override
    @Transactional(readOnly = true)
    public SysDept selectById(String id) {

        return sysDeptMapper.selectById(id);
    }

    /**
     * 查询组织机构列表
     * 
     * @param param
     * @return 
     */
    @Override
    @Transactional(readOnly = true)
    public List<SysDept> selectList(SysDeptParam param) {
    
        QueryWrapper<SysDept> queryWrapper = new QueryWrapper<>();
        
        //关键字查询
        if(StringUtils.isNotBlank(param.getKeywords())) {
            queryWrapper.and(wr->{
                wr.or().like("dept_code", param.getKeywords());
                wr.or().like("dept_name", param.getKeywords());
            });
        }
        
        queryWrapper.like(StringUtils.isNotBlank(param.getDeptCodeKey()), "dept_code", param.getDeptCodeKey());
        queryWrapper.eq(StringUtils.isNotBlank(param.getDeptCode()), "dept_code", param.getDeptCode());
        queryWrapper.like(StringUtils.isNotBlank(param.getDeptNameKey()), "dept_name", param.getDeptNameKey());
        queryWrapper.eq(StringUtils.isNotBlank(param.getDeptName()), "dept_name", param.getDeptName());
        queryWrapper.eq(Objects.nonNull(param.getStatus()), "status", param.getStatus());
        queryWrapper.ne(StringUtils.isNotBlank(param.getNeId()), "id", param.getNeId());

        return sysDeptMapper.selectList(queryWrapper);
    }

    /**
     * 新增组织机构
     * 
     * @param sysDept 
     * @return 
     */
    @Override
    @Transactional
    public int insert(SysDept sysDept) {
    
        return sysDeptMapper.insert(sysDept);
    }

    /**
     * 修改组织机构
     * 
     * @param sysDept 
     * @return 
     */
    @Override
    @Transactional
    public int update(SysDept sysDept) {
    
        return sysDeptMapper.updateById(sysDept);
    }

    /**
     * 删除组织机构信息
     * 
     * @param idStr ID，多个用逗号分隔
     * @return 
     */
    @Override
    @Transactional
    public int deleteById(String idStr) {
        if(StringUtils.isBlank(idStr)) {
            return 0;
        }
        
        ValidateUtil.validDataUsed("sys_dept", "parent_id", idStr, "机构有子节点，不可删除");
        ValidateUtil.validDataUsed("sys_dept_user", "dept_id", idStr, "机构下有用户，不可删除");
        
        return sysDeptMapper.deleteBatchIds(StringUtils.str2List(idStr));
    }
    
    
}
